package com.webermagem.DAO;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.webermagem.util.HibernateUtil;

public abstract class GenericDAO{
		
		public void save(Session session, Object objeto) {
			
			if (session == null){
				session = HibernateUtil.getSessionFactory().openSession();
			}
			
			Transaction t = session.beginTransaction();
			
			session.saveOrUpdate(objeto);
			
			session.flush();
			
			t.commit();
			
		}
		public Object getByID(Session session, long id, Object objeto) {
			
			if (session == null){
				session = HibernateUtil.getSessionFactory().openSession();
			}
			
			return  session.load(objeto.getClass(), id);
		}
		@SuppressWarnings("rawtypes")
		public List<?> list(Session session, String parametroQuery) {
			
			if (session == null){
				session = HibernateUtil.getSessionFactory().openSession();
			}
			
			Transaction t = session.beginTransaction();
			List lista = session.createQuery(parametroQuery).list();
			
			session.flush();
			
			t.commit();
			return lista;
		}
		public void remove(Session session, Object objeto) {
			
			if (session == null){
				session = HibernateUtil.getSessionFactory().openSession();
			}
			
			Transaction t = session.beginTransaction();
			session.delete(objeto);
			
			session.flush();
			
			t.commit();
			
		}
		public void update(Session session, Object objeto) {
			
			if (session == null){
				session = HibernateUtil.getSessionFactory().openSession();
			}
			
			Transaction t = session.beginTransaction();
			session.update(objeto);
			
			session.flush();
			
			t.commit();
			
		}

}
